<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="实验ID" prop="trialId">
        <el-input
          v-model="queryParams.trialId"
          placeholder="请输入实验ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应急预案描述" prop="planDescription">
        <el-input
          v-model="queryParams.planDescription"
          placeholder="请输入应急预案描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="版本号" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['human:plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['human:plan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['human:plan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['human:plan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="临床试验应急预案ID" align="center" prop="planId" />
      <el-table-column label="实验" align="center" prop="trialName" />
      
      <el-table-column label="应急预案" align="center" prop="planText">
    <template slot-scope="scope">
      <el-button 
      size="mini" 
      type="text" 
      icon="el-icon-view" 
      @click="handleViewConsent(scope.row.planText)">
      查看
      </el-button>
    </template>
    </el-table-column>
    
      <el-table-column label="应急预案描述">
        <template slot-scope="scope">
          <el-button size="mini"
          type="text" 
          icon="el-icon-view" 
         @click="viewPlanDescription(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      
     
    <el-table-column label="应急预案状态">
        <template slot-scope="scope">
          <el-button size="mini"
          type="text" 
          icon="el-icon-view" 
         @click="viewPlanStatus(scope.row)">查看</el-button>
        </template>
      </el-table-column>

      <el-table-column label="版本号" align="center" prop="version" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['human:plan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['human:plan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改临床试验应急预案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="实验">
             <el-select v-model="form.trialId" placeholder="请选择实验">
                <el-option
                  v-for="item in trialOptions"
                  :key="item.trialId"
                  :label="item.trialName"
                  :value="item.trialId"
                ></el-option>
              </el-select> 
        </el-form-item>
        <el-form-item label="应急预案" prop="planText">
          <FileUpload v-model="form.planText"/>
        </el-form-item>
        <el-form-item label="应急预案描述" prop="planDescription">
          <el-input v-model="form.planDescription" placeholder="请输入应急预案描述" />
        </el-form-item>
         <el-form-item label="应急预案状态" prop="planStatus">
          <el-input v-model="form.planStatus" placeholder="请输入应急预案状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 应急预案描述弹窗部分 -->
    <el-dialog title="应急预案描述" :visible.sync="dialogVisible" width="50%">
      <p>{{ currentPlanDescription }}</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>


    <!-- 应急预案状态弹窗部分 -->
    <el-dialog title="应急预案状态" :visible.sync="dialogVisible" width="50%">
      <p>{{ currentPlanStatus }}</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listPlan, getPlan, delPlan, addPlan, updatePlan } from "@/api/human/plan";
import {getTrials} from "@/api/human/trial";
export default {
  name: "Plan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 临床试验应急预案表格数据
      //所有试验
      trialOptions: [],
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trialId: null,
        planDescription: null,
        planStatus: null,
        createdAt: null,
        createdBy: null,
        updatedAt: null,
        updatedBy: null,
        deletedAt: null,
        deletedBy: null,
        deletedFlag: null,
        version: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      dialogVisible: false, // 控制弹窗显示与否
      currentPlanDescription: '' ,// 当前显示的应急预案描述
      currentPlanStatus: '' //当前显示的应急预案状态
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询临床试验应急预案列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows.filter(item => item.deletedFlag !== 1);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        trialId: null,
        planDescription: null,
        planStatus: null,
        createdAt: null,
        createdBy: null,
        updatedAt: null,
        updatedBy: null,
        deletedAt: null,
        deletedBy: null,
        deletedFlag: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加临床试验应急预案";
      getTrials().then(response => {
        this.trialOptions = response.data;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planId = row.planId || this.ids
      getPlan(planId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改临床试验应急预案";
      });
      getTrials().then(response => {
        this.trialOptions = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planId != null) {
            updatePlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$modal.confirm('是否确认删除临床试验应急预案编号为"' + planIds + '"的数据项？').then(function() {
        return delPlan(planIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('human/plan/export', {
        ...this.queryParams
      }, `plan_${new Date().getTime()}.xlsx`)
    },

     /** 查看应急预案 */
  handleViewConsent(planText) {
    if (!planText) {
      this.$modal.msgError("应急预案路径为空！");
      return;
    }
    if (planText.startsWith('http') || planText.startsWith('https')) {
      window.open(planText, '_blank');
    } else {
      this.$modal.msgWarning("暂不支持查看本地文件！");
    }
  },

  // 点击“查看”按钮时调用此方法，显示对应的应急预案描述
    viewPlanDescription(row) {
      this.currentPlanDescription = row.planDescription;  // 获取当前行的应急预案描述
      this.dialogVisible = true; // 显示弹窗
    },
    // 点击“查看”按钮时调用此方法，显示对应的应急预案状态
    viewPlanStatus(row) {
      this.currentPlanStatus = row.planStatus;  // 获取当前行的应急预案状态
      this.dialogVisible = true; // 显示弹窗
    }



  }
};
</script>
